package tree;

/**
 * 获得树的深度 如果根节点不存在，深度为0
 *
 * 如果树只有左子树，那么深度就是当前的深度1+左子树的深度 如果树只有右子树
 *
 * 左子树右子树比较大小
 */
public class treeDepth {


  public int getTreeDepth(IntTreeNode root) {
    if (root == null) {
      return 0;
    }

    int left = getTreeDepth(root.getLeft());
    int right = getTreeDepth(root.getRight());

    return (left > right) ? (left + 1) : (right + 1);

  }


  public static void main(String args[]) {
    IntTreeNode root = new IntTreeNode(10);
    root.setLeft(new IntTreeNode(6));
    root.getLeft().setLeft(new IntTreeNode(4));
    root.getLeft().setRight(new IntTreeNode(8));
    root.getLeft().getRight().setLeft(new IntTreeNode(7));
    root.getLeft().getRight().setRight(new IntTreeNode(9));
    root.setRight(new IntTreeNode(14));
    root.getRight().setLeft(new IntTreeNode(12));
    root.getRight().setRight(new IntTreeNode(16));

    treeDepth treeDepth = new treeDepth();
    System.out.println(treeDepth.getTreeDepth(root));

  }
}
